import handleBaseUrl from './index'
import { isValidKey } from '@/utils/isValidKey'

const reducer = (state = { ...handleBaseUrl.state }, action: { type: string; val: string & object }) => {
  const newState = JSON.parse(JSON.stringify(state))

  for (const key in handleBaseUrl.actionNames) {
    if (isValidKey(key, handleBaseUrl.actionNames)) {
      if (action.type === handleBaseUrl.actionNames[key]) {
        handleBaseUrl.actions[handleBaseUrl.actionNames[key] as keyof typeof handleBaseUrl.actions](newState, action)
        break
      }
    }
  }
  return newState
}

export default reducer
